/*
 * Copyright (C) Antonio Ramon Morcillo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package soft.xitxarello.su.helpers;

import soft.xitxarello.su.loaders.AppListLoader;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

/**
 * Helper class to look for interesting changes to the installed applications so
 * that the loader can be updated.
 * 
 * @see android.content.BroadcastReceiver
 */
public class PackageIntentReceiver extends BroadcastReceiver {

    // A custom Loader that loads all of the uninstallable applications.
    private final AppListLoader loader;

    /**
     * Base class for code that will receive intents sent by sendBroadcast(). We
     * are interested on package actions.
     * 
     * @param loader loads all of the uninstallable applications.
     */
    public PackageIntentReceiver(AppListLoader loader) {

        this.loader = loader;

        // New Application Installed.
        IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);

        // Old Application UnInstalled.
        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);

        // Add a new Intent data scheme to match against "package".
        filter.addDataScheme("package");

        // Register Receiver.
        this.loader.getContext().registerReceiver(this, filter);

        // Register for events related to sdcard installation.
        IntentFilter sdFilter = new IntentFilter();

        // Application Installed on the sd card and available.
        sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);

        // Application Installed on the sd card and unavailable.
        sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);

        // Register Receiver.
        this.loader.getContext().registerReceiver(this, sdFilter);
    }

    /**
     * Overrides a superclass method. This method is called when the
     * BroadcastReceiver is receiving an Intent broadcast. During this time you
     * can use the other methods on BroadcastReceiver to view/modify the current
     * result values.
     * 
     * @param context Interface to global information about an application
     *            environment.
     * @param intent An intent is an abstract description of an operation to be
     *            performed.
     */
    @Override
    public void onReceive(Context context, Intent intent) {

        // Tell the loader about the change.
        loader.onContentChanged();
    }

}
